import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:fluttertoast/fluttertoast.dart';

class FileRoute extends StatefulWidget {
  var _content = '使用drawPath(Path,Paint)方法画的，所有Path存放在一个arraylist之中。现在想在onTouchEvent方法中实现橡皮功能，手指触摸选中笔画并清除之。有使用Paint的setStrokeWidth方法，因此画出来的笔画有粗细。如何判断手指触点坐标是否在形成的笔画图形之内？最核心问题在于**怎么确定笔画形成的那块区域**？注：橡皮擦清除整条独立的笔画，类似于Google keep里的绘图那样，不是用背景色绘制触摸轨迹下面是代码用于储存path和画笔类型的 fingerPath类：';
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return FileRouteState();
  }
}

void _saveFile(String content) async {
  var path = await getExternalStorageDirectory();
  String filePath = '${path.path}/flutter.txt';
  File file = File(filePath);
  file.writeAsString(content).then((onValue) {
    Fluttertoast.showToast(msg: '保存成功:$filePath');
  }).catchError((onError) {
    Fluttertoast.showToast(msg: '保存失败：${onError.toString()}');
  });
}

Future<String> _readFile() async {
  var path = await getExternalStorageDirectory();
  String filePath = '${path.path}/flutter.txt';
  File file = File(filePath);
  return file.readAsString();
}

Widget _widget() {
  return FutureBuilder(
    future: _readFile(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        if (snapshot.hasError) {
          return Text('读取失败');
        } else {
          return Text(snapshot.data.toString());
        }
      } else {
        return CircularProgressIndicator();
      }
    },
  );
}

class FileRouteState extends State<FileRoute> {
  var _isRead = false;
  TextEditingController _controller = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    var them = Theme.of(context);
    var readWidget = _isRead?_widget():Text('准备读取内容');
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('文件操作'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.symmetric(vertical: 5,horizontal: 30),
              child: TextField(
                controller: _controller,
                autofocus: true,
                decoration: InputDecoration(
                  labelText: '文本内容',
                  isDense: true,
                  contentPadding: EdgeInsets.all(1)
                ),
              ),
            ),
            FlatButton(
              onPressed: () {
                _saveFile(_controller.text);
              },
              child: Text('存储'),
              color: them.accentColor,
            ),
            FlatButton(
              onPressed: () {
                setState(() {
                  _isRead = !_isRead;
                });
              },
              child: Text('读取内容'),
              color: them.accentColor,
            ),
            Padding(
              padding: EdgeInsets.all(15),
              child: readWidget,
            )
          ],
        ),
      ),
    );
  }
}
